package org.sky.auto.driver;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.sky.auto.base.ConfigParser;

import com.opera.core.systems.OperaDriver;
@Deprecated
public class Driver implements IDriver{
	static Logger logger =Logger.getLogger(Driver.class);
	//WebDriver driver;
	Browser browser;
	Object engine;
	public  Object getEngine(){
		return engine;
	}
	
	public Driver(Object engine){
		if(engine==null){
			this.engine=new InternetExplorerDriver();
		}else{
			this.engine=engine;
		}
	}
	
	public static Driver IEEngine(){
		Driver driver = new Driver(new InternetExplorerDriver());
		driver.browser=Browser.IE;
		return driver;
	}
	public static Driver FirefoxEngine(){
		Driver driver = new Driver(new FirefoxDriver());
		driver.browser=Browser.Firefox;
		return driver;
	}
	public static Driver ChromeEngine(){
		Driver driver = new Driver(new ChromeDriver());
		driver.browser=Browser.Chrome;
		return driver;
	}
	public static Driver SafariEngine(){
		Driver driver = new Driver(new SafariDriver());
		driver.browser = Browser.Safari;
		return driver;
	}
	public static Driver HtmlUnitEngine(){
		Driver driver = new Driver(new HtmlUnitDriver());
		driver.browser= Browser.HtmlUnit;
		return driver;
	}
	
	public static Driver OperaEngine(){
		Driver driver = new Driver(new OperaDriver());
		driver.browser=Browser.Opera;
		return driver;
	}
	
	public Browser getBrowser() {
		return browser;
	}

	public void setBrowser(Browser browser) {
		this.browser = browser;
	}

	public static Driver WebDriverEngine(Browser browser){
		switch(browser){
		case IE:
			return IEEngine();
		case Firefox:
			return FirefoxEngine();
		case Chrome:
			return ChromeEngine();
		case HtmlUnit:
			return HtmlUnitEngine();
		case Safari:
			return SafariEngine();
		case Opera:
			return OperaEngine();
		case RemoteChrome:
			return RemoteChromeEngine();
		case RemoteFirefox:
			return RemoteFirefoxEngine();
		case RemoteHtmlUnit:
			return RemoteHtmlUnitEngine();
		case RemoteIE:
			return RemoteIEEngine();
		case RemoteOpera:
			return RemoteOperaEngine();
		case RemoteSafari:
			return RemoteSafariEngine();
		default:
			return null;
		}
	}
	
	public static Driver RemoteIEEngine(){
		Driver driver = null;
		try {
			driver = new Driver(new RemoteWebDriver(new URL(ConfigParser.getIENode()), DesiredCapabilities.internetExplorer()));
		} catch (MalformedURLException e) {
			logger.error("实例化url出错，检查一下url格式是否正确，格式为：http://192.168.1.1:4444");
			e.printStackTrace();
		}
		driver.browser=Browser.RemoteIE;
		return driver;
	}
	
	public static Driver RemoteFirefoxEngine(){
		Driver driver = null;
		try {
			driver = new Driver(new RemoteWebDriver(new URL(ConfigParser.getFirefoxNode()), DesiredCapabilities.firefox()));
		} catch (MalformedURLException e) {
			logger.error("实例化url出错，检查一下url格式是否正确，格式为：http://192.168.1.1:4444");
			e.printStackTrace();
		}
		driver.browser=Browser.RemoteFirefox;
		return driver;
	}
	
	public static Driver RemoteChromeEngine(){
		Driver driver = null;
		try {
			driver = new Driver(new RemoteWebDriver(new URL(ConfigParser.getChromeNode()), DesiredCapabilities.chrome()));
		} catch (MalformedURLException e) {
			logger.error("实例化url出错，检查一下url格式是否正确，格式为：http://192.168.1.1:4444");
			e.printStackTrace();
		}
		driver.browser=Browser.RemoteChrome;
		return driver;
	}
	
	public static Driver RemoteSafariEngine(){
		Driver driver = null;
		try {
			driver = new Driver(new RemoteWebDriver(new URL(ConfigParser.getSafariNode()), DesiredCapabilities.safari()));
		} catch (MalformedURLException e) {
			logger.error("实例化url出错，检查一下url格式是否正确，格式为：http://192.168.1.1:4444");
			e.printStackTrace();
		}
		driver.browser=Browser.RemoteSafari;
		return driver;
	}
	
	public static Driver RemoteOperaEngine(){
		Driver driver = null;
		try {
			driver = new Driver(new RemoteWebDriver(new URL(ConfigParser.getOperaNode()), DesiredCapabilities.opera()));
		} catch (MalformedURLException e) {
			logger.error("实例化url出错，检查一下url格式是否正确，格式为：http://192.168.1.1:4444");
			e.printStackTrace();
		}
		driver.browser=Browser.RemoteOpera;
		return driver;
	}
	
	
	public static Driver RemoteHtmlUnitEngine(){
		Driver driver = null;
		try {
			driver = new Driver(new RemoteWebDriver(new URL(ConfigParser.getHtmlUnitNode()), DesiredCapabilities.htmlUnit()));
		} catch (MalformedURLException e) {
			logger.error("实例化url出错，检查一下url格式是否正确，格式为：http://192.168.1.1:4444");
			e.printStackTrace();
		}
		driver.browser=Browser.RemoteHtmlUnit;
		return driver;
	}

	@Override
	public WebDriver getWebDriver() {
		return (WebDriver)getEngine();
	}
	
}
